
/*
sessionStorage.setItem("userId","134852213520081106"); 
sessionStorage.setItem("name","任丽琴");
sessionStorage.setItem("jobnumber","0");
sessionStorage.setItem("deptId","55088496"); 
*/

//sessionStorage.setItem("userId","1304442705697854");
var thesum=0;    //待办事项返回数量
var resdata=[];  //待办事项返回数据
var sendData = {};  

if(sessionStorage.getItem("name")){ 
	$('#name').text(sessionStorage.getItem("name"));	
	sendData.jobnumber =sessionStorage.getItem("jobnumber");
	sendData.userId =sessionStorage.getItem("userId");	
	onload();
}else{
	var _config ;
	$.ajax({
		type : "post",
		data:JSON.stringify({"pageUrl":location.href.replace(/\&/g,'%26')}),
		contentType: "application/json; charset=utf-8",
		async: false,
		url : "../dingding/getconfig",
		dataType : "json",
		success : function(suc) {
			_config = suc.config;
		}
	}); 
//	dd.config({
//		agentId : _config.agentid,
//		corpId : _config.corpId,
//		timeStamp : _config.timeStamp,
//		nonceStr : _config.nonceStr,
//		signature : _config.signature,
//		jsApiList : [ 'runtime.info', 'biz.contact.choose',
//				'device.notification.confirm', 'device.notification.alert',
//				'device.notification.prompt', 'biz.ding.post',
//				'biz.util.openLink' ]
//	});
	dd.ready(function() {
		//channel runtime  requestOperateAuthCode requestAuthCode
	    dd.runtime.permission.requestAuthCode({
	        corpId: _config.corpId,
	        onSuccess: function(result) {
	        	getuserId(result);               
//	         
	        },
	        onFail : function(err) {
	        	alert("错误"+JSON.stringify(err))
	        }
	 
	    });
	});
	function getuserId(result){
		 //$("body").text('https://oapi.dingtalk.com/user/getuserinfo?code=' + result.code + '&access_token='+ _config.token);
		$.ajax({
			url : '../dingding/dd',
			type : 'POST',
			data:JSON.stringify({"code":result.code,"token":_config.token}),
			contentType: "application/json; charset=utf-8",
			dataType : "json",
			success : function(suc) {
			    if(suc.status == '1002'){
			    	sessionStorage.setItem("userId",suc.userJson.userid);
			    	sessionStorage.setItem("name",suc.userJson.name);
			    	sessionStorage.setItem("jobnumber",suc.userJson.jobnumber);
			    	sessionStorage.setItem("deptId",suc.userJson.department);
			    	$('#name').text(sessionStorage.getItem("name"));
					sendData.planuserid =sessionStorage.getItem("userId");
					setthenum();
			    	onload();
			    }else{
			    	alert(suc.msg);
			    }

			},
			error : function(xhr, errorType, error) {
					alert(errorType + ', ' + error);
				}
		});
	}
	dd.error(function(err) {
		alert('dd error: ' + JSON.stringify(err));
	});
}
function addClick(){
	$('#dataTable  tbody tr').on('click',function(){		
		//var  shopcode=$(this).find("td").eq(0).text()+'.'+$(this).find("td").eq(1).text();
		//var url='pricetagapply?shopcode='+shopcode;		
		sessionStorage.setItem("shopcode",$(this).find("td").eq(0).text());
		sessionStorage.setItem("shopname",$(this).find("td").eq(1).text());	
		var url='pricetagapply'	;
	    window.location.href=url;
	});
}

function onload(){
   	$.ajax({            
	url: "../../busswork/getSelectBody"
	,type:'post'
	,async: false
	,contentType: "application/json; charset=utf-8"
	,data:JSON.stringify(sendData) 
	,dataType:'json'
	,success: function(res){		
		var  tbhtml="";
		for(var p in res.data){
			tbhtml=tbhtml+"<tr ><td>"+res.data[p].shopcode+"</td><td>"+res.data[p].shopname+"</td></tr>";			
		}	    
		$('#dataTable tbody').html(tbhtml);
		addClick();
	}
	});
}
